#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

vector<string>::iterator end;

bool isless(const vector<string>::iterator &a, const vector<string>::iterator &b)
{
	vector<string>::iterator it1 = a;
	vector<string>::iterator it2 = b;
	for(; it1!=end && it2!=end; it1++, it2++)
	{
		if(*it1 < *it2)
			return true;
		if(*it1 > *it2)
			return false;
	}
	if(it1==end)
		return true;
	return false;
}



int main()
{
	vector<string> texts;
	vector<string>::iterator it;
	vector<string>::iterator it2;
	vector<vector<string>::iterator> its;	
	cout << "input texts" << endl;
	string s;
	while(cin>>s)
		texts.push_back(s);	
	end = texts.end();
	for(it=texts.begin(); it!=texts.end(); it++)
		its.push_back(it);	
	sort(its.begin(), its.end(), isless);

	cout << "=====================================" << endl;	
	vector<vector<string>::iterator>::iterator itit;
	for(itit=its.begin(); itit!=its.end(); itit++)
	{
		for(it=*itit; it!=end; it++)
			cout << *it << " ";
		cout << endl;	
	}
	cout << "=====================================" << endl;	

	int maxlen = 0;
	vector<string>::iterator it_b;
	for(itit=its.begin(); itit!=its.end()-1; itit++)
	{
		int len = 0;
		it = *itit;
		it2 = *(itit+1);
		while(it+len!=end && it2+len!=end && *(it+len)==*(it2+len))
			len++;
		if(len > maxlen)
		{
			maxlen = len;
			it_b = it;
		}
	}
	for(int i=0; i<maxlen; i++)
		cout << *(it_b+i) << " ";

	cin.get();
	return 0;
}
